package com.hengtiansoft.lfy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.adapter.ExerciseHistoryAdapter;
import com.hengtiansoft.lfy.base.BaseActivity;
import com.hengtiansoft.lfy.entity.EvaluatorModeEntity;
import com.hengtiansoft.lfy.net.NetHelper;
import com.hengtiansoft.lfy.net.NetUtils;
import com.hengtiansoft.lfy.widget.LoadMoreListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 练习历史记录
 * Created by suyiyang on 2016/11/4.
 */

public class ExerciseCompletedActivity extends BaseActivity {
    private LoadMoreListView lv_complete;

    private SwipeRefreshLayout refresh_layout;

    private List<EvaluatorModeEntity> list;

    private ExerciseHistoryAdapter adapter;

    private ImageView iv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercisecompleted);
        lv_complete = (LoadMoreListView) findViewById(R.id.lv_complete);
        refresh_layout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ExerciseCompletedActivity.this.finish();
            }
        });
        list = new ArrayList<>();
        adapter = new ExerciseHistoryAdapter(this,list);
        lv_complete.setAdapter(adapter);

        lv_complete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EvaluatorModeEntity modeEntity = adapter.getModeEntry(position);
                if(modeEntity == null){
                    Toast.makeText(ExerciseCompletedActivity.this,"",Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(ExerciseCompletedActivity.this,ExerciseHistoryActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("mode",modeEntity);
                intent.putExtra("bundle",bundle);
                startActivity(intent);
            }
        });

        //上拉更多
        lv_complete.setInterface(new LoadMoreListView.ILoadListener() {
            @Override
            public void onLoad() {
//                getList();
            }
        });

        //下拉加载
        refresh_layout.setProgressViewOffset(false, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()));
        refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getList();
            }
        });
        getList();
        refresh_layout.setRefreshing(true);
    }

    private void getList(){
        NetHelper.getCompleteHistory(this, new NetUtils.NetCallBack<List<EvaluatorModeEntity>>() {
            @Override
            public void success(List<EvaluatorModeEntity> rspData) {
                Log.d("tag","rspData = "+rspData.toString());
                refresh_layout.setRefreshing(false);
                lv_complete.loadComplete();
                adapter.setData(rspData);
            }

            @Override
            public void failed(String msg) {
                lv_complete.loadComplete();
                refresh_layout.setRefreshing(false);
                Toast.makeText(ExerciseCompletedActivity.this,getString(R.string.pull_to_refresh_network_error),Toast.LENGTH_SHORT).show();
            }
        });
    }
}
